﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IService.Utility;
using IService.Model;
using IService.Business;

namespace IService.User_center
{
    public partial class EditMySupplyPurchase : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            SupplyPurchase info = DTVBusiness.SupplyPurchaseManager.GetSupplyPurchaseById(Convert.ToInt32(SupplyPurchaseId));
            Dictionary<string, string> resultVideo = new Dictionary<string, string>();
            resultVideo.Add("Video1", "");
            resultVideo.Add("Video2", "");
            resultVideo.Add("Video3", "");
            VideoList = resultVideo;

            //显示TAB(采购/供应)
            if (info.Type == 1)
            {
                //供应
                //隐藏采购
                this.tab2.Style.Add("display", "none");
                //邦定下拉框
                string[] chkSelect = info.ProductType.Split(',');
                foreach (string select in chkSelect)
                {
                    for (int i = 0; i < this.cblSupplyProductType.Items.Count; i++)
                    {
                        if (string.Compare(this.cblSupplyProductType.Items[i].Value, select, true) == 0)
                        {
                            this.cblSupplyProductType.Items[i].Selected = true;
                        }
                    }
                }
                this.txtSupplyProductName.Text = info.ProductName;
                this.txtSupplyInfoTitle.Text = info.InfoTitle;
                this.txtareaSupplyProductDetailt.Value = Server.HtmlDecode(info.ProductDetail);
            }
            else
            {
                //采购
                //隐藏供应
                this.tab1.Style.Add("display", "none");

                //邦定下拉框
                string[] chkSelect = info.ProductType.Split(',');
                foreach (string select in chkSelect)
                {
                    for (int i = 0; i < this.cblPurchaseProductType.Items.Count; i++)
                    {
                        if (string.Compare(this.cblPurchaseProductType.Items[i].Value, select, true) == 0)
                        {
                            this.cblPurchaseProductType.Items[i].Selected = true;
                        }
                    }
                }
                this.txtPurchaseProductName.Text = info.ProductName;
                this.txtPurchaseInfoTitle.Text = info.InfoTitle;
                this.txtareaPurchaseProductDetail.Value = Server.HtmlDecode(info.ProductDetail);

                //采购产品图片
                string[] imgs = info.ProductPicture.Split('&');
                if (imgs.Length > 0)
                {
                    for (int i = 0; i < imgs.Length; i++)
                    {
                        if (i == 0)
                        {
                            imgProduct1.ImageUrl = imgs[i];
                            hdImgProduct1.Value = imgs[i];
                        }
                        else if (i == 1)
                        {
                            imgProduct2.ImageUrl = imgs[i];
                            hdImgProduct2.Value = imgs[i];
                        }
                        else
                        {
                            imgProduct3.ImageUrl = imgs[i];
                            hdImgProduct3.Value = imgs[i];
                        }
                    }
                }

                //采购产品视频
                string[] videos = info.ProductVideo.Split('&');
                if (videos.Length > 0)
                {
                    if (videos.Length >= 1 && !string.IsNullOrEmpty(videos[0]))
                    {
                        resultVideo["Video1"] = videos[0];
                        hdImgProduct4.Value = videos[0];
                        imgProduct4.ImageUrl = "../images/video_view.jpg";
                    }

                    if (videos.Length >= 2 && !string.IsNullOrEmpty(videos[1]))
                    {
                        resultVideo["Video2"] = videos[1];
                        hdImgProduct5.Value = videos[1];
                        imgProduct5.ImageUrl = "../images/video_view.jpg";
                    }

                    if (videos.Length >= 3 && !string.IsNullOrEmpty(videos[2]))
                    {
                        resultVideo["Video3"] = videos[2];
                        hdImgProduct6.Value = videos[2];
                        imgProduct6.ImageUrl = "../images/video_view.jpg";
                    }
                    VideoList = resultVideo;
                }

            }

            this.RequestType = info.RequestType;
            this.CreateDate = info.CreateDate;
            this.lblSupplyPurchaseType.Text = GetSupplyPurchaseType(info.RequestType, info.Type);
            this.Label1.Text = GetSupplyPurchaseType(info.RequestType, info.Type);
        }

        /// <summary>
        /// 供应信息提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSupply_Click(object sender, EventArgs e)
        {
            this.lblErrorSupply.Text = "";
            //获取选择的checkbox
            string chkSelect = "";
            for (int i = 0; i < this.cblSupplyProductType.Items.Count; i++)
            {
                if (this.cblSupplyProductType.Items[i].Selected == true)
                    chkSelect += this.cblSupplyProductType.Items[i].Value + ",";
            }
            if (chkSelect != "")
                chkSelect = chkSelect.Substring(0, chkSelect.Length - 1);

            string productType = chkSelect;
            string productName = this.txtSupplyProductName.Text.Trim();
            string infoTitle = this.txtSupplyInfoTitle.Text.Trim();
            string productDetail = Server.HtmlEncode(this.txtareaSupplyProductDetailt.Value);
            //检查信息是否完整
            if (string.IsNullOrEmpty(productType))
            {
                this.lblErrorSupply.Text = "<font color='red'>请选择产品类型！</font>";
            }
            else if (string.IsNullOrEmpty(productName))
            {
                this.lblErrorSupply.Text = "<font color='red'>请输入名称！</font>";
            }
            else if (string.IsNullOrEmpty(infoTitle))
            {
                this.lblErrorSupply.Text = "<font color='red'>清输入信息标题！</font>";
            }
            else
            {
                //初始化对象
                SupplyPurchase info = new SupplyPurchase();
                info.ID = Convert.ToInt64(SupplyPurchaseId);
                info.ProductType = productType;
                info.ProductName = productName;
                info.InfoTitle = infoTitle;
                info.ProductDetail = productDetail;
                info.UserID = SessionState.CurrentUser.ID;
                info.Type = 1;
                info.RequestType = RequestType;
                info.CreateDate = CreateDate;
                info.UpdateDateTime = DateTime.Now;
                info.ProductPicture = "";
                info.ProductVideo = "";
                //开始保存对象到数据库中
                bool hassuccess = info.Save();
                //保存成功
                if (hassuccess)
                {
                    JsUtil.Alert("供应信息提交成功！", this.Page);
                    //ClientScript.RegisterStartupScript(GetType(), "SUBMITTED", "alert('供应信息提交成功!');window.returnValue=true;window.close();", true);
                    JsUtil.RefreshParent("MySupplyPurchaseList.aspx", this.Page);
                    this.txtSupplyProductName.Text = "";
                    this.txtSupplyInfoTitle.Text = "";
                    this.txtareaSupplyProductDetailt.Value = "";
                    for (int i = 0; i < this.cblSupplyProductType.Items.Count; i++)
                    {
                        this.cblSupplyProductType.Items[i].Selected = false;
                    }
                    this.hdImgProduct1.Value = "";
                    this.hdImgProduct2.Value = "";
                    this.hdImgProduct3.Value = "";
                }
                else
                {
                    //保存失败
                    JsUtil.Alert("供应信息提交失败！", this.Page);
                }
            }
        }

        /// <summary>
        /// 采购信息提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPurchase_Click(object sender, EventArgs e)
        {
            this.lblErrorPurchase.Text = "";
            //获取选择的checkbox
            string chkSelect = "";
            for (int i = 0; i < this.cblPurchaseProductType.Items.Count; i++)
            {
                if (this.cblPurchaseProductType.Items[i].Selected == true)
                    chkSelect += this.cblPurchaseProductType.Items[i].Value + ",";
            }
            if (chkSelect != "")
                chkSelect = chkSelect.Substring(0, chkSelect.Length - 1);

            string productType = chkSelect;
            string productName = this.txtPurchaseProductName.Text.Trim();
            string infoTitle = this.txtPurchaseInfoTitle.Text.Trim();
            string productDetail = Server.HtmlEncode(this.txtareaPurchaseProductDetail.Value);
            //检查信息是否完整
            if (string.IsNullOrEmpty(productType))
            {
                this.lblErrorPurchase.Text = "<font color='red'>请选择产品类型！</font>";
            }
            else if (string.IsNullOrEmpty(productName))
            {
                this.lblErrorPurchase.Text = "<font color='red'>请输入名称！</font>";
            }
            else if (string.IsNullOrEmpty(infoTitle))
            {
                this.lblErrorPurchase.Text = "<font color='red'>清输入信息标题！</font>";
            }
            else
            {
                //初始化对象
                SupplyPurchase info = new SupplyPurchase();
                info.ID = Convert.ToInt64(SupplyPurchaseId);
                info.ProductType = productType;
                info.ProductName = productName;
                info.InfoTitle = infoTitle;
                info.ProductDetail = productDetail;
                info.UserID = SessionState.CurrentUser.ID;
                info.Type = 2;
                info.RequestType = RequestType;
                info.CreateDate = CreateDate;
                info.UpdateDateTime = DateTime.Now;

                //获取上传的图片URL
                string strPic = "";
                if (!string.IsNullOrEmpty(hdImgProduct1.Value))
                {
                    strPic += hdImgProduct1.Value + '&';
                }
                if (!string.IsNullOrEmpty(hdImgProduct2.Value))
                {
                    strPic += hdImgProduct2.Value + '&';
                }
                if (!string.IsNullOrEmpty(hdImgProduct3.Value))
                {
                    strPic += hdImgProduct3.Value;
                }
                info.ProductPicture = strPic;

                //获取上传视频的URL
                string strVideo = "";
                if (!string.IsNullOrEmpty(hdImgProduct4.Value))
                {
                    strVideo += hdImgProduct4.Value + '&';
                }
                if (!string.IsNullOrEmpty(hdImgProduct5.Value))
                {
                    strVideo += hdImgProduct5.Value + '&';
                }
                if (!string.IsNullOrEmpty(hdImgProduct6.Value))
                {
                    strVideo += hdImgProduct6.Value;
                }
                info.ProductVideo = strVideo;

                //开始保存对象到数据库中
                bool hassuccess = info.Save();
                //保存成功
                if (hassuccess)
                {
                    //JsUtil.Alert("采购信息提交成功！", this.Page);
                    ClientScript.RegisterStartupScript(GetType(), "SUBMITTED", "alert('采购信息提交成功!');window.returnValue=true;window.close();", true);
                    this.txtPurchaseProductName.Text = "";
                    this.txtPurchaseInfoTitle.Text = "";
                    this.txtareaPurchaseProductDetail.Value = "";
                    for (int i = 0; i < this.cblPurchaseProductType.Items.Count; i++)
                    {
                        this.cblPurchaseProductType.Items[i].Selected = false;
                    }
                    this.hdImgProduct4.Value = "";
                    this.hdImgProduct5.Value = "";
                    this.hdImgProduct6.Value = "";
                }
                else
                {
                    //保存失败
                    JsUtil.Alert("采购信息提交失败！", this.Page);
                }
            }
        }

        protected int RequestType
        {
            get
            {
                try
                {
                    return Convert.ToInt32(this.ViewState["RequestType"]);
                }
                catch
                {
                    return 1;
                }
            }
            set
            {
                this.ViewState["RequestType"] = value;
            }
        }

        protected DateTime CreateDate
        {
            get
            {
                try
                {
                    return Convert.ToDateTime(this.ViewState["CreateDate"]);
                }
                catch
                {
                    return DateTime.Now;
                }
            }
            set
            {
                this.ViewState["CreateDate"] = value;
            }
        }

        protected string SupplyPurchaseId
        {
            get
            {
                return base.Request["SupplyPurchaseId"];
            }
        }

        //获取需求分类
        public string GetSupplyPurchaseType(int requestType, int type)
        {
            string result = "";

            switch (requestType)
            {
                case 1:
                    if (type == 1)
                        result = "产品采购";
                    else
                        result = "产品供应";
                    break;
                case 2:
                    if (type == 1)
                        result = "技术求购";
                    else
                        result = "技术供应";
                    break;
                case 3:
                    if (type == 1)
                        result = "股权投资";
                    else
                        result = "股权转让";
                    break;
                default:
                    result = "";
                    break;
            }
            return result;
        }

        protected Dictionary<string, string> VideoList
        {
            get
            {
                return (this.ViewState["VideoList"] as Dictionary<string, string>);
            }
            set
            {
                this.ViewState["VideoList"] = value;
            }
        }
    }
}